/*
 * @Author: GY19531 418941369@qq.com
 * @Date: 2022-08-29 14:06:00
 * @LastEditors: GY19531 418941369@qq.com
 * @LastEditTime: 2022-09-08 21:11:19
 * @FilePath: \one-web\src\pages\RemindManage\services\ManageService.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getBaseUrl } from '@/services/urlconst';
import { useRequest } from 'umi';
import { getTenantID, requestFunComponFiex } from '@/utils';
import { useMemo, useCallback, useState } from 'react';
import { loadMdicMap } from '@/services/BaseLayout';
import { getSearchColumns } from '../mconfig';
let baseUrl = getBaseUrl();
// let baseUrl = `http://192.168.3.21:9007`;
// let baseUrl = getBaseUrl()
const RemindManageService = (permissions: Record<string, any>) => {
  /**获取数据字典loadMdicMap**/
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap([
      `${getTenantID()}1401`,
      `${getTenantID()}1402`,
      `${getTenantID()}1403`,
      `${getTenantID()}1004`,
    ]);
  });
  /****/
  const [manageTableParams, setManageTableParams] = useState<any>({});
  const tableParamsCallback = useCallback(
    (parObj: any) => {
      setManageTableParams(parObj);
    },
    [manageTableParams],
  );
  /**searchList**/
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList,
      searchList: getSearchColumns(loadMdicMapList, manageTableParams),
    };
  }, [loadMdicMapList?.data]);
  /**请求数据**/
  const { tableProps, data, run, loading } = useRequest(
    (requestParams: any) => {
      const dataGridURL = `${baseUrl}/notifyController/dataGrid/${requestParams?.params}`;
      let postParams = {
        current: 1,
        pageSize: 10,
        sort: 'signTime',
        dicTypeIds: `${getTenantID()}1401`,
        ...requestParams,
      };
      tableParamsCallback(postParams);
      console.log('postParams==', postParams);
      return requestFunComponFiex('POST', dataGridURL, postParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );
  const refreshRequest = useCallback((requestParams: any) => {
    if (requestParams?.createTime) {
      const [createTimeStart, createTimeEnd] = requestParams.createTime;
      requestParams['createTimeStart'] = createTimeStart.format(
        'YYYY-MM-DD HH:mm:ss',
      );
      requestParams['createTimeEnd'] = createTimeEnd.format(
        'YYYY-MM-DD HH:mm:ss',
      );
      delete requestParams['createTime'];
    }
    if (requestParams?.notifyTime) {
      const [startTime, endTime] = requestParams.notifyTime;
      requestParams['startTime'] = startTime.format('YYYY-MM-DD HH:mm:ss');
      requestParams['endTime'] = endTime.format('YYYY-MM-DD HH:mm:ss');
      delete requestParams['notifyTime'];
    }

    delete requestParams['createrFormNameDept'];
    delete requestParams['createrFormNameUser'];
    if (requestParams?.createrID && requestParams?.createrID?.length > 0) {
      requestParams['createrID'] = requestParams?.createrID?.toString();
    } else {
      delete requestParams['createrID'];
    }
    run({
      ...requestParams,
    });
  }, []);
  const remindDataInfo = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    refreshRequest,
    manageTableParams,
    ...remindDataInfo,
    ...loadMapList,
  };
};
export { RemindManageService };
